package edu.poly.game.util;

public class BoundaryValidationUtil {
	public static boolean isWithinBoundary(final Coordinate from, final Coordinate to, 
			final Coordinate checkFrom, final Coordinate checkTo) {
		//nothing to check here, since one value is null
		if(from == null || to == null || checkFrom == null) {
			return false;
		}
		
		//proceed with boundary check
		int fromX = from.getXCoordinate();
		int fromY = from.getYCoordinate();
		int toX = to.getXCoordinate();
		int toY = to.getYCoordinate();
		
		int checkFromX = checkFrom.getXCoordinate();
		int checkFromY = checkFrom.getYCoordinate();
		int checkToX = checkTo.getXCoordinate();
		int checkToY = checkTo.getYCoordinate();
		
		// or operator has short circuit built in
		if(checkFromX >= fromX && checkFromX <= toX &&
				checkFromY >= fromY && checkFromY <= toY) {
			return false;
		} else if (checkToX >= fromX && checkToX <= toX &&
				checkToY >= fromY && checkToY <= toY) {
			return false;
		} else if (fromX >= checkFromX && fromX <= checkToX &&
				fromY >= checkFromY && fromY <= checkToY) {
			return false;
		} else if (checkFromY <= fromY && checkToY >= toY && fromX == toX) {
			return false;
		} else if (checkFromX <= fromX && checkToX >= toX && fromY == toY) {
			return false;
		}
		
		//all previous check passed, so we're good, coordinate is within boundary
		return true;
	}
	
	public static boolean isWithinBoundary(final Coordinate from, final Coordinate to, 
			final Coordinate checkFrom) {
		//nothing to check here, since one value is null
		if(from == null || to == null || checkFrom == null) {
			return false;
		}
		
		//proceed with boundary check
		int fromX = from.getXCoordinate();
		int fromY = from.getYCoordinate();
		int toX = to.getXCoordinate();
		int toY = to.getYCoordinate();
		
		int checkFromX = checkFrom.getXCoordinate();
		int checkFromY = checkFrom.getYCoordinate();
		
		// or operator has short circuit built in
		if(fromX == toX) {
			//this checks across
			return fromX == checkFromX;
		} else if(checkFromX < fromX || checkFromX > toX) {
			return false;
		} else if (fromY == toY) {
			return fromY == checkFromY;
		} else if (checkFromY < fromY || checkFromY > toY) {
			return false;
		}
		
		//all previous check passed, so we're good, coordinate is within boundary
		return true;
	}
}
